package com.ruoyi.stock2.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;

public class Test1 {

    private static final Logger log = LoggerFactory.getLogger(Test1.class);

    public static String Transform_to_Binary(int decimal){
        StringBuilder sb = new StringBuilder(8);
        for(int i = 7;i >= 0; i--){
            int a =  decimal >>> i & 1;
            sb.append(a);
        }
        return sb.toString();
    }

    /**
     * 以字节为单位读取文件，常用于读二进制文件，如图片、声音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字节为单位读取文件内容，一次读一个字节：");
            // 一次读一个字节
            in = new FileInputStream(file);
            int tempbyte;
            StringBuffer sbt = new StringBuffer(0);
            while ((tempbyte = in.read()) != -1) {
//                System.out.println(Transform_to_Binary(tempbyte/2));
                sbt.append(Transform_to_Binary(tempbyte/2));
            }
            in.close();

/*            StringBuilder sb = new StringBuilder("1234567890abcdefjhijklmnoptrstuvwxyz11");
            int a = sb.length() % 4;
            for(int i= 0 ; i < sb.length() - 4 ; i = i + 4){
                System.out.println(sb.substring(i, i + 4));
            }
            System.out.println(sb.substring(sb.length()-a));
            System.out.println(sb.substring(sb.length()-a).length());*/
            //压缩算法
            //System.out.println(sbt.length());
            //yasuo(sbt);
            StringBuffer sb1 = yasuo2(new StringBuffer(sbt.substring(0,100000)));
//            StringBuffer sb2 = yasuo2(sb1);
//            StringBuffer sb3 = yasuo2(sb2);
//            System.out.println(sb1.length());
//            System.out.println(sb2.length());
//            System.out.println(sb3.length());
            //保存


        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }

    public static StringBuffer yasuo2(StringBuffer sb_0){
        System.out.println(sb_0);
        StringBuffer[] setp_1 =  ysf2(sb_0,"01010101");
        StringBuffer sb0101_and_value = new StringBuffer();
        sb0101_and_value.append(setp_1[0]);
        return sb0101_and_value;
    }
    public static StringBuffer[] ysf2(StringBuffer sb_1, String sf){
        StringBuffer sb_value = new StringBuffer(0);
        StringBuffer sb_0101 = new StringBuffer(0);
        int length0 = sb_1.length() % sf.length();
        for(int i = 0; i < sb_1.length() - sf.length(); i = i + sf.length()){
            String str4 = null;
            try {
                str4 = sb_1.substring(i, i + sf.length());
            }catch (StringIndexOutOfBoundsException s){
                s.printStackTrace();
            }
            if(sf.equals(str4)) {
                sb_0101.append("1");
            }else{
                sb_0101.append("0");
                sb_value.append(str4);
            }
        }
        sb_value.append( sb_1.substring(sb_1.length() - length0) );
        System.out.println(sb_0101.length() +" "+ sb_value.length());
        return new StringBuffer[]{sb_0101, sb_value};
    }

    /**
     * 显示输入流中还剩的字节数
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "d:/html2pdf/微信图片_20200406141007.jpg";
//        String fileName = "d:/html2pdf/整个屏幕_51Record.mp4";
        Test1.readFileByBytes(fileName);
//        Test1.readFileByChars(fileName);
//        Test1.readFileByLines(fileName);
//        Test1.readFileByRandomAccess(fileName);
    }

    public static StringBuffer yasuo2_old(StringBuffer sb_0){

        StringBuffer[] setp_1 =  ysf2_old(sb_0,"0000");

        StringBuffer[] setp_2 =  ysf2_old(setp_1[1],"0001");

        StringBuffer[] setp_3 =  ysf2_old(setp_2[1],"0101");

        StringBuffer[] setp_4 =  ysf2_old(setp_3[1],"0100");

        StringBuffer sb0101_and_value = new StringBuffer();
        sb0101_and_value.append(setp_1[0]);
        sb0101_and_value.append(setp_2[0]);
        sb0101_and_value.append(setp_3[0]);
        sb0101_and_value.append(setp_4[0]);
        sb0101_and_value.append(setp_4[1]);
        return sb0101_and_value;
    }
    public static StringBuffer[] ysf2_old(StringBuffer sb_1, String sf){
        StringBuffer sb_value = new StringBuffer(0);
        StringBuffer sb_0101 = new StringBuffer(0);
        int length0 = sb_1.length() % sf.length();
        for(int i = 0; i < sb_1.length() - sf.length(); i = i + sf.length()){
            String str4 = null;
            try {
                str4 = sb_1.substring(i, i + sf.length());
            }catch (StringIndexOutOfBoundsException s){
                s.printStackTrace();
            }
            if(sf.equals(str4)){
                sb_0101.append("01");
            }else{
                sb_0101.append(0);
                sb_value.append(str4);
            }
        }
        sb_value.append( sb_1.substring(sb_1.length() - length0) );
        System.out.println(sb_0101.length() +" "+ sb_value.length());
        return new StringBuffer[]{sb_0101, sb_value};
    }

}
